package com.sinai.mshab.client.ui.basic;

import java.util.Iterator;

import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RichTextArea;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.sinai.mshab.client.util.Answer;
import com.sinai.mshab.client.ui.basic.richtexttoolbar.RichTextToolbar;

public class RichTextComponent extends TextAreaComponent {

	VerticalPanel richTextArea;
	RichTextArea area;
	RichTextToolbar toolbar;

	public RichTextComponent(String dbfieldName, String tableName,
			String description) {
		super(dbfieldName, tableName, description);
	}

	public RichTextComponent(String dbfieldName, String tableName,
			String description, boolean horizontal) {
		super(dbfieldName, tableName, description, horizontal);
	}

	public RichTextComponent(String dbfieldName, String tableName,
			String description, String quantifier) {
		super(dbfieldName, tableName, description, quantifier);
	}

	public RichTextComponent(String dbfieldName, String tableName,
			String description, String quantifier, boolean horizontal) {
		super(dbfieldName, tableName, description, quantifier, horizontal);
	}

	public RichTextComponent(String dbfieldName, String tableName,
			String description, String quantifier, String error_msg) {
		super(dbfieldName, tableName, description, quantifier, error_msg);
	}

	public RichTextComponent(String dbfieldName, String tableName,
			String description, String quantifier, String error_msg,
			boolean horizontal) {
		super(dbfieldName, tableName, description, quantifier, error_msg,
				horizontal);
	}

	protected void initializeWidget(String dbfieldName, String tableName,
			String description, String quantifier, String error_msg,
			boolean horizontal) {

		richTextArea = new VerticalPanel();
		area = new RichTextArea();
		toolbar = new RichTextToolbar(area);

		richTextArea.add(toolbar);
		richTextArea.add(area);
		area.setHeight("14em");
		area.setWidth("100%");
		toolbar.setWidth("100%");
		richTextArea.setWidth("100%");

		super.initialize(dbfieldName, tableName, description, quantifier,
				error_msg, richTextArea, horizontal);

		// set the widget to be the actual text area rather then the vertical
		// panel.
		widget = area;
	}

	public Answer getAnswer() {
		Answer answer = new Answer();
		answer.add(dbfieldName, tableName, getText());
		answer.add(hiddenFields);
		return answer;
	}

	/**
	 * Populate this widget with FormData using an Answer object
	 * 
	 * @param answer
	 *            the answer object containing the data.
	 */
	public boolean populate(Answer answer) {

		// if answer is null, do nothing
		if (answer == null) {
			return false;
		}

		// walking through all the fields-values of answer
		Iterator field_names = answer.getHashMap().keySet().iterator();
		while (field_names.hasNext()) {
			String field_name = (String) field_names.next();

			// if field name matches our widget's field name, use this value
			if (field_name.equalsIgnoreCase(dbfieldName)) {
				String value = (String) answer.getHashMap().get(field_name);
				setText(value);

				// apply mask if it exists
				if (mask != null) {
					setText(mask.applyMask(getText()));
				}

				// if we populate the widget with nothing, return false.
				if (value == null || value.trim().length() == 0) {
					return false;
				}

				return true;
			}
		}
		return false;
	}

	/**
	 * Sets the contents of the RichTextComponent to message which can be HTML
	 * or text.
	 * 
	 * @param message
	 *            The text that will go to the text field.
	 */
	public void setText(String message) {
		((RichTextArea) widget).setHTML(message);
	}

	/**
	 * Returns the text of the textbox.
	 * 
	 * @param message
	 *            The text that is in the textbox
	 */
	public String getText() {
		return ((RichTextArea) widget).getHTML();
	}

	public Object clone() {
		BasicFormComponent t;
		t = new RichTextComponent(dbfieldName, tableName, description,
				quantifier, error_msg);

		t.setMask(this.mask);
		t.setValidator(this.validator);

		if (subquestion)
			t.setAsSubQuestion();

		return t;
	}
	
	public HorizontalPanel toPrint() {
		HorizontalPanel printableVersion = new HorizontalPanel();

		// add the description label.
		if (description_label != null) {

			// make a copy of the label.
			Label description = new Label(description_label.getText());
			description.setStyleName(description_label.getStyleName());
			printableVersion.add(description);
		}

		// add a new label with the answer.
		if (area.getHTML() != null) {

			HTML answerLabel = new HTML(area.getHTML());
			printableVersion.add(answerLabel);
		}

		// add the quantifier.
		if (quantifier_label != null) {

			// make a copy of the label.
			Label quantifier = new Label(quantifier_label.getText());
			quantifier.setStyleName(quantifier_label.getStyleName());
			printableVersion.add(quantifier);
		}

		return printableVersion;
	}

	/**
	 * When to focus is lost validate the field.
	 */
	public void onFocus(Widget sender) {
		String name = ((RichTextArea) widget).getStyleName();
		((RichTextArea) widget).setStyleName(name + "_focus");
	}

	/**
	 * When to focus is lost validate the field.
	 */
	public void onLostFocus(Widget sender) {

		String name = ((RichTextArea) widget).getStyleName();
		if (name.indexOf("_focus") != -1) {
			((RichTextArea) widget).setStyleName(name.substring(0, name
					.length()
					- "_focus".length()));
		}

		validate();
	}

	/**
	 * Returns the actual {@link RichTextArea} widget.
	 * 
	 * @return the actual {@link RichTextArea} widget.
	 */
	public RichTextArea getRichTextArea() {
		return area;
	}

	/**
	 * Returns the {@link RichTextToolbar} used by this widget.
	 * 
	 * @return the {@link RichTextToolbar} used by this widget.
	 */
	public RichTextToolbar getRichTextToolbar() {
		return toolbar;
	}
}
